<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class AdminUserIdentity extends UserIdentity
{
    private $status = 1;
    public $role_id;

	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
        $record=User::model()->findByAttributes(array('email'=>$this->username , 
                                                        'status' => $this->status,
                                                        ));
        if($record===null)
        {
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        }
        else if(trim($record->password) != md5(trim($this->password)))
        {
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        }
        else if($record->status==0 )
        {
            $this->errorCode=  self::ERROR_USERNAME_BLOCKED;
        }
        else
        {
            $this->_id=$record->id;
            $this->role_id = $record->role_id;
            $this->_isAdmin = true;
            $this->errorCode=self::ERROR_NONE;
            Yii::app()->session['LOGGED_USER'] = $record;

            if(isset($_POST['AdminLoginForm']['rememberMe'])){
                if($_POST['AdminLoginForm']['rememberMe']==1){
                    $expire = time() + 7*24*60*60;
                    $array[LOGIN] = $record->email;
                    $array[PASS] = $record->password;
                    setcookie (COOKIE_ADMIN,  json_encode($array),$expire);
                }
            }         
 
            
        }
        
        return !$this->errorCode;
	}

}